package com.app.buss.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.app.buss.R;
import com.app.buss.bean.MarketBean;
import com.app.buss.ui.MaketDetailActivity;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class MarketAdapter extends RecyclerView.Adapter<MarketAdapter.MViewHolder> {
    private List<MarketBean> datas;
    private Context context;

    public MarketAdapter(List<MarketBean> datas, Context context){
     this.datas = datas;
     this.context = context;
    }

    @NonNull
    @Override
    public MViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_market, parent, false);
        return new MViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull MViewHolder holder, int position) {
        holder.title.setText(datas.get(position).getPair());
        holder.des.setText(datas.get(position).getBuy());
        holder.num.setText(datas.get(position).getSell());
        holder.pairs.setText(datas.get(position).getSignal());
        if(datas.get(position).getSignal().equals("Buy")){
            holder.des.setTextColor(context.getResources().getColor(R.color.green));
            holder.num.setTextColor(context.getResources().getColor(R.color.green));
            holder.pairs.setTextColor(context.getResources().getColor(R.color.green));
        }else {
            holder.des.setTextColor(context.getResources().getColor(R.color.red));
            holder.num.setTextColor(context.getResources().getColor(R.color.red));
            holder.pairs.setTextColor(context.getResources().getColor(R.color.red));
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                context.startActivity(new Intent(context, MaketDetailActivity.class).putExtra("data",datas.get(position)));
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    static class MViewHolder extends RecyclerView.ViewHolder{
        TextView title;
        TextView des;
        TextView num;
        TextView pairs;

        MViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.tv_name);
            des = itemView.findViewById(R.id.tv_buy);
            num = itemView.findViewById(R.id.tv_sell);
            pairs = itemView.findViewById(R.id.tv_signal);

        }
    }
}
